package com.example.wms;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.example.wms.domain.PcjsonRootBean;
import com.example.wms.domain.Rows;
import com.example.wms.utils.OkHttpUtils;
import com.google.gson.Gson;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private RecyclerView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = findViewById(R.id.listview);


    }

    @Override
    protected void onStart() {
        super.onStart();
        OkHttpUtils.getInstance().getOrder( new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                System.out.println("响应失败");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.body() != null) {
                    String str = response.body().string();

                    Gson gson = new Gson();
                    PcjsonRootBean pcjsonRootBean = gson.fromJson(str, PcjsonRootBean.class);
                    if (pcjsonRootBean!=null ){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                System.out.println("str"+pcjsonRootBean.getRows());
                                OrderRecyclerViewAdapter OrderRecyclerViewAdapter =new OrderRecyclerViewAdapter(pcjsonRootBean.getRows(),MainActivity.this);
                                // 点击扫码按钮
                                OrderRecyclerViewAdapter.setOnLaunchClickListener(new OrderRecyclerViewAdapter.OnLaunchClickListener() {
                                    @Override
                                    public void onClick(int position, Rows rows) {
                                        SharedPreferences sharedPreferences = getSharedPreferences("cc", Context.MODE_PRIVATE);
                                        SharedPreferences.Editor edit = sharedPreferences.edit();
                                        edit.putString("order_id", String.valueOf(rows.getId()));
                                        edit.putString("p_id", String.valueOf(rows.getPid()));
                                        edit.putString("extract_id", String.valueOf(rows.getExtractionCode()));
                                        edit.apply();
                                        startActivity(new Intent(MainActivity.this,MainActivity2.class));
                                    }

                                });
                                listview.setAdapter(OrderRecyclerViewAdapter);
                                listview.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
                            }
                        });
                    }

                }
            }
        });


    }
}
